﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.cust.qjJSDBE;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForARBillUpdate : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //应收单
            UFIDA.U9.AR.ARBill.ARBillHead ar = key.GetEntity() as UFIDA.U9.AR.ARBill.ARBillHead;
            if (ar == null) return;

            #region 应收单状态同步结算单

            cust.qjJSDBE.JSDEntity jsd = cust.qjJSDBE.JSDEntity.Finder.Find($"ARDocNo='{ar.DocNo}'");
            if (jsd == null) return;
            //判断结算单是否联动
            using (ISession session = Session.Open()) {
                if (jsd.ARLinkAge == JSDLinkAge.Yes)
                {
                    if ( ar.DocStatus == BillStatusEnum.Approving)
                    {
                        jsd.DocState = JSState.Approving;
                    }
                    else if (ar.DocStatus == BillStatusEnum.Approved)
                    {
                        jsd.DocState = JSState.Approved;
                    }else if( ar.DocStatus == BillStatusEnum.Opened)
                    {
                        jsd.DocState = JSState.Opened;
                    }

                }
                session.Commit();
            }
              

          

            #endregion

        }

    }
}
